
import info.Course;
import info.LectureRoom;
import info.Message;
import info.SpecialSchedule;

import java.awt.Color;
import java.awt.List;

import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

import javax.swing.ComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import client.*;

import common.ChatIF;

public class OfficeGUI extends JFrame implements ChatIF
{
	OfficerClient clientt;
	
	OfficePanel myOfficePanel;
	LecRoomPanelA lecP;
	TermPanel termP;
	private LectureRoom room[]=new LectureRoom[10];
	private int numberOfLecRoom=0;
		
	public OfficeGUI(String host, int port, ChatIF clientUI)
	{
		super("Officer");
		JTabbedPane jtp = new JTabbedPane(); //�����θ���
		setSize(1500, 1000);
		try {
			clientt=new OfficerClient(host, port, clientUI);
		} catch (IOException e) {
			
			e.printStackTrace();
		}
		setVisible(true);
		setResizable(false);
	    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	    
	    myOfficePanel = new OfficePanel();    
	    termP = new TermPanel();
	    lecP = new LecRoomPanelA();
	    	    
	    jtp.addTab("Schedule", termP);
	    jtp.addTab("LecRoom", lecP);
	      	    
	    getContentPane().add(jtp);
	   
	    jtp.setBounds(0, 50, 900, 810);
	}		
	
	@Override
	public void display(String message)
	{
				
	}
	
	public class OfficePanel extends JPanel
	{	
		OfficePanel()
		{	
		//	setPanel();
		//	setButton();
		//	setImage();
		//	setTextField();
		//	setLabel();
		}
		public void setPanel()
		{
		   setLayout(null);	    	
		   setBackground(Color.pink);			 
		}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

class LecRoomPanelA extends JPanel implements ChatIF  
{
	OfficerClient client;

	private JLabel location[]=new JLabel[10];
	private JLabel number[]=new JLabel[10];
	private JLabel capacity[]=new JLabel[10];
	
	private JLabel lecAvail=new JLabel("AvailableLecroom");
	
	
	private JTextField locationT[] = new JTextField[10];
	private JTextField numberT[] = new JTextField[10];
	private JTextField capacityT[] = new JTextField[10];
	
	private JTextArea lecroomlist= new JTextArea();
		
	private JButton loadLecAvail;
	private JButton saveButton, modifyButton;
	
	private String lecRoomInfo[] = new String[30];
	
	public LecRoomPanelA()
	{
		setLayout(null);
		this.setBackground(Color.lightGray);
		setLabel();
		setButton();
		setTextField();
		
		try {
			client=new OfficerClient("localhost", 5555, this);
		} catch (IOException e) {
		
			e.printStackTrace();
		}	
	}
	
	public void setTextField()
	{
		int j=0;
		int k=0;
		for(int i=1; i<10; i++)
		{
			locationT[i]=new JTextField("");
			numberT[i] =new JTextField("");
			capacityT[i]=new JTextField("");
			
			add(locationT[i]);
			add(numberT[i]);
			add(capacityT[i]);
		
			locationT[i].setBounds(120+j, 45+k, 70, 20);
			k=k+50;
			numberT[i].setBounds(120+j, 45+k, 70, 20);
			k=k+50;
			capacityT[i].setBounds(120+j, 45+k, 70, 20);
			k=k+100;
		
			if(i%3==0)
			{
				j=j+300;
				k=0;
			}		
		}			
		add(lecroomlist);
		lecroomlist.setBounds(850, 50, 400, 800);
	}
	
	public void setButton()
	{
		modifyButton=new JButton("Modify");
		saveButton=new JButton("Save");
		loadLecAvail=new JButton("LoadLecRoom");
		
		add(modifyButton);
		add(saveButton);
		add(loadLecAvail);
		
		modifyButton.setBounds(600, 700, 100, 30);
		saveButton.setBounds(720, 700, 100, 30);
		loadLecAvail.setBounds(1050, 20, 125, 30);
		
		ButtonHandler handler = new ButtonHandler();
		
		modifyButton.addActionListener(handler);
		saveButton.addActionListener(handler);
		loadLecAvail.addActionListener(handler);	
	}
	
	public void setLabel()
	{
		int j=0;
		int k=0;
		for(int i=1; i<10; i++)
		{
			location[i]=new JLabel("Location");
			number[i] =new JLabel("RoomNo");
			capacity[i]=new JLabel("Capacity");
			
			add(location[i]);
			add(number[i]);
			add(capacity[i]);
		
			location[i].setBounds(30+j, 30+k, 50, 50);
			k=k+50;
			number[i].setBounds(30+j, 30+k, 50, 50);
			k=k+50;
			capacity[i].setBounds(30+j, 30+k, 100, 50);
			k=k+100;
		
			if(i%3==0)
			{
				j=j+300;
				k=0;
			}
		}	
		add(lecAvail);
		lecAvail.setBounds(875, 20, 125, 30);
	}
	
	private class ButtonHandler implements ActionListener
	{
		public void actionPerformed(ActionEvent event)
		{
			
			if(event.getActionCommand().equals("Modify"))
			{
				for(int i=1; i<10; i++)
				{
					locationT[i].setEditable(true);
					numberT[i].setEditable(true);
					capacityT[i].setEditable(true);
				}		
			}			
			else if(event.getActionCommand().equals("Save"))
			{
				int j=0;
				
				try {
					clientt.sendToServer("#initnumofRoom");
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				
				for(int i=1; i<10; i++)
				{					
					if(locationT[i].getText().isEmpty() && numberT[i].getText().isEmpty() && capacityT[i].getText().isEmpty()) //�� ���ǽ��� pass
						continue;
					else if(locationT[i].getText().isEmpty() || numberT[i].getText().isEmpty() || capacityT[i].getText().isEmpty()) //����
					{
						JOptionPane.showMessageDialog(LecRoomPanelA.this,String.format("Error.", event.getActionCommand()));
						return;
					}
															
				}				
				for(int i=1; i<10; i++)
				{
					locationT[i].setEditable(false);
					numberT[i].setEditable(false);
					capacityT[i].setEditable(false);	
				}
				numberOfLecRoom=0;		
				JOptionPane.showMessageDialog(LecRoomPanelA.this,String.format("Completed.", event.getActionCommand()));
				for(int i=1; i<10; i++)
				{
					if(locationT[i].getText().isEmpty() && numberT[i].getText().isEmpty() && capacityT[i].getText().isEmpty())
						continue;
					else //Send Information of LecRoom to server
					{
						
						room[j]=new LectureRoom(numberT[i].getText(), Integer.parseInt(capacityT[i].getText()), locationT[i].getText());
						
						Message msg=new Message();
						
						msg.msgHeader="#addroom";
						msg.msgBody=room[j];
						
						System.out.println(room[j].getLocation());
						
						numberOfLecRoom++;
						j++;
						try 
						{
							clientt.sendToServer(msg);
							System.out.println(numberOfLecRoom);
						} 
						catch (IOException e)
						{
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				}
			}		
			
			//if push this button load file and display available lecroom
			else if(event.getActionCommand().equals("LoadLecRoom"))
			{
				BufferedReader Fread = null;
			    String exp = null;
			    int p=0;
			    try {	     
			       Fread = new BufferedReader(new FileReader("src/lecroomtext/availablelecroom.txt"));
			       
			        while((exp = Fread.readLine()) != null)
			        {

			           lecRoomInfo[p]=exp;
			           p++;	   
			        }      
			        
			        setLecRoomList(p);
			    } 
			    catch(IOException e) 
			    {
			        System.out.println("Error in reading file");
			        e.printStackTrace();
			    } finally {

			        if(Fread != null) 
			        	try { Fread.close(); } 
			        catch(Exception e) {}
			    }
			}
		}

	}
	
	//set the exist(not the Officer want to make available) LecRoom
	public void setLecRoomList(int p)
	{
		lecroomlist.setText("");
		

		for(int k=0; k<p; k++)
			lecroomlist.append(lecRoomInfo[k]+"\n");
		
	}

	@Override
	public void display(String message) {
		
		
	}	
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class TermPanel extends JPanel
{		
	private JTextField startDateText;
	private JTextField endDateText;
	//private JTextField startDayText;
	//private JTextField endDayText;
	//private JTextField startMonthText;
	//private JTextField endMonthText;
	
	private JTextField specialNameText;
	private JTextField specialStartDateText;
	private JTextField specialEndDateText;
	private JTextField specialStartTimeText;
	private JTextField specialEndTimeText;
	
	private JTextField regularNameText;
	private JTextField regularDayText;
	private JTextField regularStartTimeText;
	private JTextField regularEndTimeText;
	private JTextField regularCodeText;
	private JTextField regularLecRoomText;
	private JTextField regularProfText;
	private JTextField regularNumText;
	private JComboBox regularLookCombo;
	
	private JTextArea termDetailText= new JTextArea();
	
	private JTextField dayText;
	
	private JButton okButton;
	private JButton cancelButton;
	
	private JButton seeButton;
	
	private JButton regularAddButton;
	private JButton regularCleanButton;
	private JButton regularLookButton;
	
	private JButton regularRegisterButton;
	private JButton regularDeleteButton;
	
	private JButton specialLookButton;
	private JButton specialDeleteButton;
	private JButton specialRegisterButton;
	private JButton specialAddButton;
	private JButton specialCleanButton;
	
	private JLabel startLabel, endLabel;
//	private JLabel startDLabel, endDLabel;
//	private JLabel startMLabel, endMLabel;
	private JLabel dayLabel;
	private JLabel testSchedule;
	
	private JLabel specialScheduleLabel;
	
	private JLabel specialNameLabel;
	private JLabel specialStartDateLabel;
	private JLabel specialEndDateLabel;
	private JLabel specialStartTimeLabel;
	private JLabel specialEndTimeLabel;
	
	private JLabel regularNameLabel;
	private JLabel regularDayLabel;
	private JLabel regularStartTimeLabel;
	private JLabel regularEndTimeLabel;
	private JLabel regularCodeLabel;
	private JLabel regularLecRoomLabel;
	private JLabel regularProfLabel;
	private JLabel regularNumLabel;
	
	private JLabel regularTimeTableLabel;
	private JLabel regularDayLabel2;
	
	private JList regularJList;
	private JList specialJList;
	
	private String selectedDay="ChooseDay";
	
	private int numberOfRegular=0;
	private int numberOfSpecial=0;
	
	//It saves a String from a file one line by line
	private String termInfo[] = new String[30];
	private String regularList[]={"empty", "empty", "empty", "empty", "empty", "empty", "empty", "empty", "empty", "empty", "empty", "empty", "empty", "empty", "empty", 
			"empty", "empty", "empty", "empty", "empty", "empty", "empty", "empty", "empty", "empty", "empty", "empty", "empty", "empty", "empty"};
	
	private String regularTempList[]={"empty", "empty", "empty", "empty", "empty", "empty", "empty", "empty", "empty", "empty", "empty", "empty", "empty", "empty", "empty", 
			"empty", "empty", "empty", "empty", "empty", "empty", "empty", "empty", "empty", "empty", "empty", "empty", "empty", "empty", "empty"};
	
	private String specialList[]={"empty", "empty", "empty", "empty", "empty", "empty", "empty", "empty", "empty", "empty", "empty", "empty", "empty", "empty", "empty", 
			"empty", "empty", "empty", "empty", "empty", "empty", "empty", "empty", "empty", "empty", "empty", "empty", "empty", "empty", "empty"};
	
	private String specialTempList[]={"empty", "empty", "empty", "empty", "empty", "empty", "empty", "empty", "empty", "empty", "empty", "empty", "empty", "empty", "empty", 
			"empty", "empty", "empty", "empty", "empty", "empty", "empty", "empty", "empty", "empty", "empty", "empty", "empty", "empty", "empty"};
		
	private String courseInfo[][]={ {"empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty"},
			{"empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty"},
			{"empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty"},
			{"empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty"},
			{"empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty"},
			{"empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty"},
			{"empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty"},
			{"empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty"}};
	
	private String specialInfo[][]={ {"empty","empty","empty","empty","empty","empty","empty","empty","empty","empty"},
			{"empty","empty","empty","empty","empty","empty","empty","empty","empty","empty"},{"empty","empty","empty","empty","empty","empty","empty","empty","empty","empty"},
			{"empty","empty","empty","empty","empty","empty","empty","empty","empty","empty"},{"empty","empty","empty","empty","empty","empty","empty","empty","empty","empty"}			
	};
	
		
	private int startDate, endDate;
	boolean schedule;
	
	private int indexOfRegularList=0;
	private int indexOfSpecialList=0;
	
	public TermPanel()
	{
		setLayout(null);
		this.setBackground(Color.lightGray);					
					
		setLabel();
		setTextField();
		setButton();		
		setComboBox();
		setList();
	}
	
	public void setLabel()
	{
		startLabel=new JLabel("Startdate");
		endLabel =new JLabel("Enddate");
		
		add(startLabel);
		add(endLabel);
		
	//	startDLabel=new JLabel("Startday");
	//	endDLabel=new JLabel("Endday");
		
	//	startMLabel=new JLabel("Startmonth");
	//	endMLabel=new JLabel("Endmonth");
		
		dayLabel=new JLabel("date");
		testSchedule=new JLabel("TestSchedule");
		regularTimeTableLabel=new JLabel("RegularTimeTable");
		regularDayLabel2=new JLabel("Day");
		
		specialNameLabel=new JLabel("ScheduleName");
		specialStartDateLabel=new JLabel("Startday");
		specialEndDateLabel=new JLabel("EndDate");
		specialStartTimeLabel=new JLabel("Starttime");
		specialEndTimeLabel=new JLabel("EndTime");
		specialScheduleLabel=new JLabel("SpecialSchedule");
		
		regularNameLabel=new JLabel("CourseName");
		regularDayLabel=new JLabel("Day");
		regularStartTimeLabel=new JLabel("Starttime");
		regularEndTimeLabel=new JLabel("EndTime");
		regularCodeLabel=new JLabel("Code");
		regularLecRoomLabel=new JLabel("LecRoom");
		regularProfLabel=new JLabel("Prof");
		regularNumLabel=new JLabel("Number");
		
	//	add(startDLabel);
	//	add(endDLabel);
	//	add(startMLabel);
	//	add(endMLabel);
		add(dayLabel);
		add(testSchedule);
		
		add(regularNameLabel);
		add(regularDayLabel);
		add(regularStartTimeLabel);
		add(regularEndTimeLabel);
		add(regularCodeLabel);
		add(regularLecRoomLabel);
		add(regularProfLabel);
		add(regularNumLabel);
		
		add(regularTimeTableLabel);
		add(regularDayLabel2);
		
		add(specialNameLabel);
		add(specialStartDateLabel);
		add(specialEndDateLabel);
		add(specialStartTimeLabel);
		add(specialEndTimeLabel);
		add(specialScheduleLabel);
		
	//	startDLabel.setBounds(30, 0, 120, 50);
	//	endDLabel.setBounds(130, 0, 120, 50);
		startLabel.setBounds(30, 50, 120, 50);
		endLabel.setBounds(130, 50, 120, 50);
		//startMLabel.setBounds(30, 120, 120, 50);
		//endMLabel.setBounds(130, 120, 120, 50);
		
		specialNameLabel.setBounds(30, 380, 150, 50);
		specialStartDateLabel.setBounds(30, 420, 120, 50);
		specialEndDateLabel.setBounds(130, 420, 120, 50);
		specialStartTimeLabel.setBounds(30, 470, 120, 50);
		specialEndTimeLabel.setBounds(130, 470, 120, 50);
		specialScheduleLabel.setBounds(560, 350, 150, 30);
		
		regularNameLabel.setBounds(30, 600, 150, 50);
		regularDayLabel.setBounds(30, 650, 150, 50);
		regularCodeLabel.setBounds(130, 650, 150, 50 );
		regularStartTimeLabel.setBounds(30, 700, 150, 50);
		regularEndTimeLabel.setBounds(130, 700, 150, 50);	
		regularProfLabel.setBounds(30, 750, 150, 50);
		regularLecRoomLabel.setBounds(130, 750, 150, 50);
		regularNumLabel.setBounds(30, 800, 150, 50);
		
		dayLabel.setBounds(1000, 20, 50, 30);
		testSchedule.setBounds(560, 20, 100, 30);
		regularTimeTableLabel.setBounds(560, 620, 150, 30);
		regularDayLabel2.setBounds(700, 600, 150, 30);
	}
	
	public void setButton()
	{
		okButton= new JButton("Apply");
		cancelButton = new JButton("Cancel");
		seeButton = new JButton("See");
		
		regularAddButton=new JButton("Add");
		regularCleanButton=new JButton("Clean");
		regularLookButton=new JButton("Look");
		
		regularRegisterButton=new JButton("Register");
		regularDeleteButton=new JButton("Delete");
		
		specialLookButton=new JButton("LookS");
		specialDeleteButton=new JButton("DeleteS");
		specialRegisterButton=new JButton("RegisterS");
		specialAddButton=new JButton("AddS");
		specialCleanButton=new JButton("CleanS");
		
		add(okButton);
		add(cancelButton);
		add(seeButton);
		
		add(regularAddButton);
		add(regularCleanButton);
		add(regularLookButton);
		add(regularRegisterButton);
		add(regularDeleteButton);
		
		add(specialLookButton);
		add(specialDeleteButton);
		add(specialRegisterButton);
		add(specialAddButton);
		add(specialCleanButton);
				
		okButton.setBounds(200, 50, 75, 30);
		cancelButton.setBounds(200, 90, 75, 30);
		seeButton.setBounds(1150, 20, 100, 30);
		
		regularAddButton.setBounds(275, 640, 100, 30);
		regularCleanButton.setBounds(275, 690, 100, 30);
		regularLookButton.setBounds(930, 600, 100, 30);
		regularRegisterButton.setBounds(1030, 600, 100, 30);
		regularDeleteButton.setBounds(1130, 600, 100, 30);
		
		specialLookButton.setBounds(900, 350, 100, 30);
		specialDeleteButton.setBounds(1025, 350, 100, 30);
		specialRegisterButton.setBounds(1150, 350, 100, 30);
		specialAddButton.setBounds(275, 450, 100, 30);
		specialCleanButton.setBounds(275, 500, 100, 30);
		
		
		ButtonHandler handler = new ButtonHandler();
		
		okButton.addActionListener(handler);
		cancelButton.addActionListener(handler);
		seeButton.addActionListener(handler);
		regularAddButton.addActionListener(handler);
		regularLookButton.addActionListener(handler);
		regularCleanButton.addActionListener(handler);
		regularRegisterButton.addActionListener(handler);
		regularDeleteButton.addActionListener(handler);
		
		specialLookButton.addActionListener(handler);
		specialDeleteButton.addActionListener(handler);
		specialRegisterButton.addActionListener(handler);
		specialAddButton.addActionListener(handler);
		specialCleanButton.addActionListener(handler);
		
	}
	
	public void setTextField()
	{			
		//시험기간입력 텍스트필드
		startDateText=new JTextField();
		endDateText=new JTextField();
		
		//startDayText=new JTextField();
		//endDayText=new JTextField();
		
		//startMonthText=new JTextField();
		//endMonthText=new JTextField();
		
		dayText=new JTextField();
		
		specialNameText=new JTextField();
		specialStartDateText=new JTextField();
		specialEndDateText=new JTextField();
		specialStartTimeText=new JTextField();
		specialEndTimeText=new JTextField();
		
		regularNameText=new JTextField();
		regularDayText=new JTextField();
		regularStartTimeText=new JTextField();
		regularEndTimeText=new JTextField();
		regularCodeText=new JTextField();
		regularLecRoomText=new JTextField();
		regularProfText=new JTextField();
		regularNumText=new JTextField();
				
		add(termDetailText);

		add(startDateText);
		add(endDateText);
	//	add(startDayText);
	//	add(endDayText);
	//	add(startMonthText);
	//	add(endMonthText);
		add(dayText);
		
		add(specialNameText);
		add(specialStartDateText);
		add(specialEndDateText);
		add(specialStartTimeText);
		add(specialEndTimeText);
		
		add(regularNameText);
		add(regularDayText);
		add(regularStartTimeText);
		add(regularEndTimeText);
		add(regularCodeText);
		add(regularProfText);
		add(regularLecRoomText);
		add(regularProfText);
		add(regularNumText);
		
		startDateText.setBounds(30, 100, 50, 20);
		endDateText.setBounds(135, 100, 50, 20);
	//	startDayText.setBounds(30, 40, 50, 20);
	//	endDayText.setBounds(135, 40, 50, 20);
	//	startMonthText.setBounds(30, 160, 50, 20);
	//	endMonthText.setBounds(135, 160, 50, 20);
		
		specialNameText.setBounds(140, 400, 200, 20);
		specialStartDateText.setBounds(30, 460, 50, 20);
		specialEndDateText.setBounds(130, 460, 50, 20);
		specialStartTimeText.setBounds(30, 510, 95, 20);
		specialEndTimeText.setBounds(130, 510, 95, 20);
		
		regularNameText.setBounds(30, 645, 200, 20);
		regularDayText.setBounds(30, 690, 50, 20);
		regularCodeText.setBounds(130, 690, 120, 20);
		regularStartTimeText.setBounds(30, 740, 70, 20);
		regularEndTimeText.setBounds(130, 740, 70, 20);
		regularProfText.setBounds(30, 790, 50, 20);
		regularLecRoomText.setBounds(130, 790, 100, 20);
		regularNumText.setBounds(30, 840, 50, 20);
		
		dayText.setBounds(1050, 30, 50, 20);
		
		termDetailText.setBounds(550, 50, 700, 250);
	}
	
	public void setComboBox()
	{
		final String dayList[]={"ChooseDay", "Mon", "Tue", "Wen", "Thu", "Fri"};
		
		regularLookCombo=new JComboBox(dayList);
		regularLookCombo.setMaximumRowCount(6);
			
		regularLookCombo.addItemListener(
			new ItemListener()
			{

				public void itemStateChanged(ItemEvent event) 
				{
					if(event.getStateChange()==ItemEvent.SELECTED)
					{
						selectedDay=event.getItem().toString();
					}
					
				}
					
			}
		);
		
		add(regularLookCombo);
		regularLookCombo.setBounds(750 ,600, 150, 30);
	}
	
	public void setList()
	{

		regularJList=new JList(regularList);
		regularJList.setVisibleRowCount(20);
		regularJList.addListSelectionListener(
				new ListSelectionListener()
				{
					public void valueChanged(ListSelectionEvent event)
					{
						indexOfRegularList=regularJList.getSelectedIndex();
					}
				});
		add(regularJList);
		regularJList.setBounds(550, 650, 700, 250);
		
		specialJList=new JList(specialList);
		specialJList.setVisibleRowCount(10);
		specialJList.addListSelectionListener(
				new ListSelectionListener()
				{
					public void valueChanged(ListSelectionEvent event)
					{
						indexOfSpecialList=specialJList.getSelectedIndex();
					}
				});
		add(specialJList);
		specialJList.setBounds(550, 380, 700, 200);
		
	}
	
	
	public void makeCourse()
	{
		Course course[]=new Course[30];
		int k=0;
		for(int i=0; i<30; i++)
		{
			if(!(courseInfo[0][i].equals("empty")))
			{
				for(int j=0; j<numberOfLecRoom; j++)
				{
					if(courseInfo[7][i].startsWith(room[j].getLocation()+"-"+room[j].getName()))
					{
						course[k]=new Course(courseInfo[0][i], courseInfo[1][i], courseInfo[2][i], courseInfo[3][i], courseInfo[4][i], courseInfo[5][i], courseInfo[6][i], room[j]);
				
						
						try {
							Message msg=new Message();
							msg.msgHeader="#addcourse";
							msg.msgBody=course[k];
							clientt.sendToServer(msg);
							//System.out.println(course[k].getLecRoom().getLocation());
							//System.out.println(course[k].getLecRoom().getName());
							//System.out.println(course[k].getLecRoom().getCapacity());
							
							k++;
							} 
						catch (IOException e) 
						{
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					
					}
						

				}
			}
		}
	}
		
	public void setCourseInfo()
	{
		boolean isSet=false;
		for(int j=0; j<30; j++)
		{

				if(courseInfo[0][j].equals("empty"))
				{
					courseInfo[0][j]=regularNameText.getText();
					courseInfo[1][j]=regularCodeText.getText();
					courseInfo[2][j]=regularDayText.getText();
					courseInfo[3][j]=regularStartTimeText.getText();
					courseInfo[4][j]=regularEndTimeText.getText();
					courseInfo[5][j]=regularProfText.getText();
					courseInfo[6][j]=regularNumText.getText();
					
					
					for(int i=0; i<numberOfLecRoom; i++)
					{
						
						System.out.println(room[i].getLocation());
						if(!(room[i].getLocation().isEmpty()))
						{
							if(regularLecRoomText.getText().startsWith(room[i].getLocation()))
							{
								courseInfo[7][j]=regularLecRoomText.getText();
							}
						}
					}
					isSet=true;
					break;
				}
			
			if(isSet==true)
				break;
		}
		isSet=false;
		for(int j=0; j<30; j++)
		{
			for(int i=0; i<8; i++)
					System.out.printf(courseInfo[i][j]+"###");				
			System.out.println("");
		}
	}
	
	public void setCourseDelete()
	{
		for(int i=0; i<30; i++)
		{
			// If Code and Day are Same => delete;
			if(courseInfo[1][i].equals(courseInfo[1][indexOfRegularList]) && courseInfo[2][i].equals(courseInfo[2][indexOfRegularList]) )
			{
				courseInfo[0][i]="empty";
				courseInfo[1][i]="empty";
				courseInfo[2][i]="empty";
				courseInfo[3][i]="empty";
				courseInfo[4][i]="empty";
				courseInfo[5][i]="empty";
				courseInfo[6][i]="empty";
				break;
			}
			
		}
		for(int j=0; j<30; j++)
		{
			for(int i=0; i<8; i++)
					System.out.printf(courseInfo[i][j]+"###");				
			System.out.println("");
		}
	}
	
	public void makeSpecial()
	{
		SpecialSchedule special[]=new SpecialSchedule[10];
		for(int i=0; i<10; i++)
		{
			if(!(specialInfo[0][i].equals("empty")))
			{
				special[i]=new SpecialSchedule(specialInfo[0][i], specialInfo[1][i], specialInfo[2][i], specialInfo[3][i], specialInfo[4][i]);
				System.out.println("i는"+i);
				System.out.println("info1"+specialInfo[0][i]+specialInfo[1][i]+specialInfo[2][i]+specialInfo[3][i]+specialInfo[4][i]);
				System.out.println("info2"+special[i].getName()+special[i].getStartDate()+special[i].getEndDate()+special[i].getStartTime()+special[i].getEndTime());
			try {
				Message msg=new Message();
				msg.msgHeader="#addschedule";
				msg.msgBody=special[i];
				clientt.sendToServer(msg);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			}
		}
		
		boolean specialTable[][]=new boolean[26][5];
		String tempTerm[]={"1A","1B","2A","2B","3A","3B","4A","4B","5A","5B","6A","6B","7A","7B","8A","8B","9A","9B","10A","10B","11A","11B","12A","12B","13A","13B"};
		String tempDay[]={"Mon","Tue","Wen","Thu","Fri"};
		
		int endNum = 0, startNum = 0;
		int dayNum = 0;
		int startDayNum=0; int endDayNum=0;
		
		for(int i=0; i<26; i++)
		{
			for(int j=0; j<5; j++)
			{
				specialTable[i][j]=false;
			}	

		}
		
	
		for(int k=0; k<numberOfSpecial; k++)
		{
			for(int i=0; i<5; i++)
			{
				if(special[k].getStartDate().equals(tempDay[i]))
				{
					dayNum=i;
				}
			}
		
		
			for(int i=0; i<26; i++)
			{
				if(special[k].getStartTime().equals(tempTerm[i]))
				{
					startNum=i;
				}
			}
		
		
			for(int i=0; i<26; i++)
			{
				if(special[k].getEndTime().equals(tempTerm[i]))
				{
					endNum=i;
				}
			}
			System.out.println("스타트타임은"+special[0].getStartTime()+"엔드타임은"+special[0].getEndTime());
			System.out.printf("start넘은 %d end넘은 %d\n",startNum, endNum);
		
			for(int i=startNum; i<=endNum; i++)
			{
				for(int j=startDayNum; j<=endDayNum; j++)
					specialTable[i][j]=false;
			}
		
			for(int i=0; i<26; i++)
			{
				for(int j=0; j<5; j++)
				{
					System.out.printf(specialTable[i][j]+"　");
				}
				System.out.println();
			}
		}
		
		Message msc=new Message();
		msc.msgHeader="addSpecialTable";
		msc.msgBody=special;
		
		//---!!!send server to Table about Special Schedule
		
/*		try {
			clientt.sendToServer(special);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}*/
	}
	

		
	public void setSpecialInfo()
	{
		boolean isSet=false;
		for(int j=0; j<10; j++)
		{

				if(specialInfo[0][j].equals("empty"))
				{
					specialInfo[0][j]=specialNameText.getText();
					specialInfo[1][j]=specialStartDateText.getText();
					specialInfo[2][j]=specialEndDateText.getText();
					specialInfo[2][j]=specialStartTimeText.getText();
					specialInfo[3][j]=specialEndTimeText.getText();
					isSet=true;
					break;
				}
			
			if(isSet==true)
				break;
		}
		isSet=false;
		for(int j=0; j<10; j++)
		{
			for(int i=0; i<4; i++)
					System.out.printf(specialInfo[i][j]+"###");				
			System.out.println("");
		}
	}
	
	public void setSpecialDelete()
	{
		for(int i=0; i<10; i++)
		{
			// If Code and Day are Same => delete;
			if(specialInfo[0][i].equals(specialInfo[1][indexOfSpecialList]) &&specialInfo[0][i].equals(specialInfo[1][indexOfSpecialList]) && specialInfo[2][i].equals(specialInfo[2][indexOfSpecialList]) )
			{
				specialInfo[0][i]="empty";
				specialInfo[1][i]="empty";
				specialInfo[2][i]="empty";
				specialInfo[3][i]="empty";
			//	specialInfo[4][i]="empty";
				break;
			}
			
		}
		for(int j=0; j<10; j++)
		{
			for(int i=0; i<4; i++)
					System.out.printf(specialInfo[i][j]+"###");				
			System.out.println("");
		}
	}
	
	private class ButtonHandler implements ActionListener
	{
		public void actionPerformed(ActionEvent event)
		{
	//Term Schedule
	//////////////////////////////////////////////////////////////////////////////		
			if(event.getActionCommand().equals("Apply"))
			{
				String start=null;
				String end=null;

				start=startDateText.getText();
				end=endDateText.getText();	
				
				if(start.isEmpty() || end.isEmpty())
				{
					JOptionPane.showMessageDialog(TermPanel.this,String.format("Please input value.", event.getActionCommand()));					
					return;
				}						
				//!!!!startDate가 null일때 exception 발생함 처리해줘야함
				startDate=Integer.parseInt(start);
				endDate=Integer.parseInt(end);
				
				System.out.println("시험기간입력");
				schedule=true;
				
				//Send date to server
				try {
					clientt.sendToServer("#adddate"+':'+startDate);
					JOptionPane.showMessageDialog(TermPanel.this,String.format("Completed", event.getActionCommand()));					
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
			else if(event.getActionCommand().equals("Cancel"))
			{
				startDateText.setText(" ");
				endDateText.setText(" ");
			}	
	
			else if(event.getActionCommand().equals("See"))
			{
				BufferedReader Fread = null;
			    String exp = null;
			    int p=0;
				if(dayText.getText().isEmpty())
				{
					JOptionPane.showMessageDialog(TermPanel.this,String.format("Please input value.", event.getActionCommand()));					
					return;
				}	
			    try {	     
			       Fread = new BufferedReader(new FileReader("src/termtext/Day"+dayText.getText()+".txt"));
			       
			        while((exp = Fread.readLine()) != null)
			        {

			           termInfo[p]=exp;
			           p++;	   
			        }      
			        
			        setTermList(p);
			    } 
			    catch(IOException e) 
			    {
			        System.out.println("Error in reading file");
			        e.printStackTrace();
			    } finally {

			        if(Fread != null) 
			        	try { Fread.close(); } 
			        catch(Exception e) {}
			    }
			}
	//////////////////////////////////////////////////////////////////////////////////////////////////		
	//regular schedule
			else if(event.getActionCommand().equals("Clean"))
			{
				regularNameText.setText("");
				regularDayText.setText("");
				regularStartTimeText.setText("");
				regularEndTimeText.setText("");
				regularCodeText.setText("");
				regularProfText.setText("");
				regularLecRoomText.setText("");
				regularProfText.setText("");
				regularNumText.setText("");
			}
			
			else if(event.getActionCommand().equals("Add"))
			{
				
				//Display ERROR if one of them is empty
				if(regularNameText.getText().isEmpty() || regularDayText.getText().isEmpty() || regularCodeText.getText().isEmpty() || regularStartTimeText.getText().isEmpty() || 
						regularEndTimeText.getText().isEmpty() || regularProfText.getText().isEmpty() || regularLecRoomText.getText().isEmpty() || regularNumText.getText().isEmpty())
				{
					JOptionPane.showMessageDialog(TermPanel.this,String.format("ERROR : Please Fill in all of Textfields.", event.getActionCommand()));					
					return;
				}
				
				System.out.println("adf");
				for(int i=0; i<30; i++)
				{
					if(regularTempList[i].equals("empty"))
					{
						regularTempList[i]=regularDayText.getText()+"　　　"+regularNameText.getText()+"　　　"+regularCodeText.getText()+"　　　"+regularStartTimeText.getText()+"　　　"+regularEndTimeText.getText()+"　　　"+regularProfText.getText()+"　　　"+regularLecRoomText.getText()+"　　　"+regularNumText.getText();
						
						System.out.println(regularDayText.getText()+"\t"+regularNameText.getText()+"\t"+regularStartTimeText.getText()+"\t"+regularEndTimeText.getText()+"\t"
								+regularCodeText.getText()+"\t"+regularProfText.getText()+"\t"+regularLecRoomText.getText()+"\t"+regularNumText.getText());
												
						JOptionPane.showMessageDialog(TermPanel.this,String.format("Added.", event.getActionCommand()));

						break;
					}
				}
				numberOfRegular++;
				setCourseInfo();
			}
			else if(event.getActionCommand().equals("Look"))
			{
				int p=0;
				//---!! open file  +selcetedDay+".txt"
				for(int i=0; i<30; i++)
				{

					if(selectedDay.equals("ChooseDay"))
					{
						JOptionPane.showMessageDialog(TermPanel.this,String.format("ERROR : choose day first and press Look Button", event.getActionCommand()));
						break;
					}
					else if(regularTempList[i].startsWith(selectedDay))
					{
						regularList[p]=regularTempList[i];
						p++;
					}	
				}
				for(int k=p; k<30; k++)
				{
					regularList[k]="empty";
				}
				regularJList.updateUI();
				regularJList.setBackground(Color.white);
			}
			
			else if(event.getActionCommand().equals("Delete"))
			{
			
				for(int i=0; i<30; i++)
				{
					if(numberOfRegular==0)
						return;
					if(regularTempList[i].equals(regularJList.getSelectedValue().toString()))
					{
						regularTempList[i]="empty";
						regularList[indexOfRegularList]="empty";
						break;
					}
					
				}
				int p=0;
				//---!! open file  +selcetedDay+".txt"
				for(int i=0; i<30; i++)
				{

					if(selectedDay.equals("ChooseDay"))
					{
						JOptionPane.showMessageDialog(TermPanel.this,String.format("ERROR : choose day first and press Look Button", event.getActionCommand()));
						break;
					}
					else if(regularTempList[i].startsWith(selectedDay))
					{
						regularList[p]=regularTempList[i];
						p++;
					}	
				}
				for(int k=p; k<30; k++)
				{
					regularList[k]="empty";
				}
				regularJList.updateUI();
				regularJList.setBackground(Color.white);
				setCourseDelete();
				numberOfRegular--;
			}
			
			else if(event.getActionCommand().equals("Register"))
			{
				if(numberOfLecRoom==0)
				{
					JOptionPane.showMessageDialog(TermPanel.this,String.format("ERROR : input lecRoom first", event.getActionCommand()));
				}
				
				if(numberOfRegular!=0)
				{
					JOptionPane.showMessageDialog(TermPanel.this,String.format("Register Completed", event.getActionCommand()));
					try {
						clientt.sendToServer("#initnumofCourse");
					} catch (IOException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					makeCourse();
				}
			}
//Special Schedule
///////////////////////////////////////////////
			else if(event.getActionCommand().equals("AddS"))
			{
				//Display ERROR if one of them is empty
				if(specialNameText.getText().isEmpty() || specialStartDateText.getText().isEmpty() || specialEndDateText.getText().isEmpty() || specialStartTimeText.getText().isEmpty() || 
						specialEndTimeText.getText().isEmpty())
				{
					JOptionPane.showMessageDialog(TermPanel.this,String.format("ERROR : Please Fill in all of Textfields.", event.getActionCommand()));					
					return;
				}
			
				for(int i=0; i<30; i++)
				{
					if(specialTempList[i].equals("empty"))
					{
						specialTempList[i]=specialNameText.getText()+"　　　"+specialStartDateText.getText()+"　　　"+specialEndDateText.getText()+"　　　"+specialStartTimeText.getText()+"　　　"+specialEndTimeText.getText();
						
						System.out.println(specialNameText.getText()+"　　　"+specialStartDateText.getText()+"　　　"+specialEndDateText.getText()+"　　　"+specialStartTimeText.getText()+"　　　"+specialEndTimeText.getText());
						

						
						JOptionPane.showMessageDialog(TermPanel.this,String.format("Added.", event.getActionCommand()));

						break;
					}
				}
				setSpecialInfo();
					specialNameText.setText("");
					specialStartDateText.setText("");
					specialEndDateText.setText("");
					specialStartTimeText.setText("");
					specialEndTimeText.setText("");
				numberOfSpecial++;
			}
			else if(event.getActionCommand().equals("CleanS"))
			{
				specialNameText.setText("");
				specialStartDateText.setText("");
				specialEndDateText.setText("");
				specialStartTimeText.setText("");
				specialEndTimeText.setText("");
			}
			else if(event.getActionCommand().equals("LookS"))
			{
				int p=0;
				//---!! open file  +selcetedDay+".txt"
				for(int i=0; i<30; i++)
				{
					if(!(specialTempList[i].equals("empty")))
					{
						specialList[p]=specialTempList[i];
						p++;
					}
					
				}
				for(int k=p; k<30; k++)
				{
					specialList[k]="empty";
				}
				specialJList.updateUI();
				specialJList.setBackground(Color.white);
			}
			else if(event.getActionCommand().equals("DeleteS"))
			{
				if(numberOfSpecial==0)
					return;
				for(int i=0; i<30; i++)
				{
					if(specialTempList[i].equals(specialJList.getSelectedValue().toString()))
					{
						specialTempList[i]="empty";
						specialList[indexOfSpecialList]="empty";
						break;
					}				
				}
				int p=0;
				//---!! open file  +selcetedDay+".txt"
				for(int i=0; i<30; i++)
				{
					if(!(specialTempList[i].equals("empty")))
					{
						specialList[p]=specialTempList[i];
						p++;
					}
					
				}
				for(int k=p; k<30; k++)
				{
					specialList[p]="empty";
				}
				
				specialJList.updateUI();
				specialJList.setBackground(Color.white);
				
				setSpecialDelete();
				numberOfSpecial--;
			}
			else if(event.getActionCommand().equals("RegisterS"))
			{
				if(numberOfSpecial!=0)
				{
					JOptionPane.showMessageDialog(TermPanel.this,String.format("Register Completed", event.getActionCommand()));
					makeSpecial();
				}
			}
///////////////////////////////////////////			
		}
	}
		private void setTermList(int p) 
		{
			termDetailText.setText("");
		
			for(int k=0; k<p; k++)
				termDetailText.append(termInfo[k]+"\n");			
		}
	}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

}